<!--概览-->
<template>
    <a-row style="padding-bottom: 1rem" v-userAuth="'B1'"><!--height: 100%;-->
        <a-row style="padding:1em 1em .5em 1em;">
            <Allstate></Allstate>
        </a-row>
        <a-row style="padding:0 1em; min-height: 81.5%;" :gutter='8'>
            <a-col :xs='24' :sm='24' :md='24' :lg='24' :xl='11'>
                <a-row :gutter='8' style="height: 100%">
                    <a-col :xs='24' :sm='24' :md='24' :lg='12' :xl='12' style="height: 51%">
                        <Cluster></Cluster>
                    </a-col>
                    <a-col :xs='24' :sm='24' :md='24' :lg='12' :xl='12' style="height: 51%">
                        <Slice></Slice>
                    </a-col>
                    <a-col :xs='24' :sm='24' :md='24' :lg='24' :xl='24' style="height: 48%">
                        <Warn></Warn>
                    </a-col>
                </a-row>
            </a-col>
            <a-col :xs='24' :sm='24' :md='24' :lg='24' :xl='13' style="height: 100%;">
                <Charts style="min-height: 74.5vh;"></Charts>
            </a-col>
        </a-row>
    </a-row>
</template>
<script>

import Allstate from '../../components/Home/Allstate'
import Othermsg from '../../components/Home/othermsg'
import Charts from '../../components/Home/charts'
import Cluster from '../../components/Home/cluster'
import Slice from '../../components/Home/slice'
import Warn from '../../components/Home/warn'

import axios from 'axios'
import '../../api/env'
export default {
    name:'index',
    components:{Allstate,Othermsg,Charts,Cluster,Slice,Warn},
    beforeRouteEnter(to, from, next) {
      if (JSON.parse(localStorage.getItem('islogin'))) {
        //取得到登录信息，进行下一步
        next();
      } else {
        //取不到登录信息(判断是否从外部登录)
        let fromUrl = window.location.href;
        let name_start_index = fromUrl.indexOf('=');
        let name_end_index = fromUrl.indexOf('&');
        let token_start_index = name_end_index + 6;
        let out_login_name = '';//从外部应用登录的登录名
        let token = '';
        if(name_start_index!=-1 && name_end_index !=-1) {
          out_login_name = fromUrl.slice(name_start_index+1, name_end_index);
        }
        token = fromUrl.slice(token_start_index+1);
        if(out_login_name && token) {
            axios.post(global.constants.website + 'user/auth',{
              token: token
            }).then(res => {
                //如果是真的token
                if(res.data.success) {
                  next(vm => {
                    vm.$store.commit('islogin', {
                      islogin: true,
                    }) 
                    vm.$store.commit('getlogin', {
                      loginuser: out_login_name
                    })
                    vm.$notification['success']({
                      message: vm.$t('Login_successful'),
                      duration: 3,
                    });
                  })
                  next();
                }else {//否则
                  this.$store.commit('islogin', {
                    islogin: false,
                  })
                  next({ path: '/' });
                }
            }).catch(err => {
                console.log(err);
            })
        }else {
          next({ path: '/' });
        }
      } 
    }
}
</script>
